package com.jsu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jsu.mapper.ActivityBarrageMapper;
import com.jsu.mapper.AgendaBarrageMapper;
import com.jsu.mapper.BarrageBanMapper;
import com.jsu.service.ActivityService;
import com.jsu.service.AgendaService;
import com.jsu.service.BarrageService;
import com.jsu.client.UserClient;
import org.example.model.activity.dto.BarrageBanDTO;
import org.example.model.activity.pojo.*;
import org.example.model.common.dtos.ResponseResult;
import org.example.model.user.pojo.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class BarrageServiceImpl implements BarrageService {

    @Autowired
    private AgendaBarrageMapper agendaBarrageMapper;
    @Autowired
    private ActivityBarrageMapper activityBarrageMapper;
    @Autowired
    private BarrageBanMapper barrageBanMapper;
    @Autowired(required = false)
    private UserClient userClient;
    @Autowired
    private AgendaService agendaService;
    @Autowired
    private ActivityService activityService;

    @Override
    public ResponseResult addBarrage(Integer sid,Integer type, String content,String userId) {
        if (type==1){
            AgendaComment agendaComment = new AgendaComment();
            agendaComment.setUserId(Integer.valueOf(userId));
            agendaComment.setAgendaId(Integer.valueOf(sid));
            //设置内容
            agendaComment.setCommentContent(content);
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sdf.format(date);
            agendaComment.setCommentTime(date);
            agendaComment.setCommentType(2);//弹幕类型
            if (agendaBarrageMapper.insert(agendaComment) == 0){
                throw new RuntimeException("发送失败！");
            }
            return ResponseResult.okResult("发送成功！");
        }
        else {
            ActivityComment activityComment = new ActivityComment();
            activityComment.setUserId(Integer.valueOf(userId));
            activityComment.setActivityId(sid);
            activityComment.setCommentContent(content);
            activityComment.setCommentTime(new Date());
            activityComment.setCommentType(2);//弹幕类型
            if (activityBarrageMapper.insert(activityComment)==0){
                throw new RuntimeException("发送失败！");
            }
            return ResponseResult.okResult("发送成功！");
        }

    }

    @Override
    public ResponseResult addBan(Integer sid, Integer type, Integer userId) {
        LambdaQueryWrapper<BarrageBan> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BarrageBan::getSid,sid)
                .eq(BarrageBan::getType,type)
                .eq(BarrageBan::getUserId,userId);
        BarrageBan barrageBan1 = barrageBanMapper.selectOne(lambdaQueryWrapper);
        if (Objects.isNull(barrageBan1)){
            BarrageBan barrageBan = new BarrageBan();
            barrageBan.setBanTime(new Date());
            barrageBan.setType(type);
            barrageBan.setSid(sid);
            barrageBan.setUserId(userId);
            barrageBanMapper.insert(barrageBan);
        }
        else {
            throw new RuntimeException("该用户已被禁言");
        }


        return ResponseResult.okResult("禁言成功");
    }

    @Override
    public ResponseResult cancleBan(Integer sid, Integer type, Integer userId) {
        LambdaQueryWrapper<BarrageBan> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BarrageBan::getSid,sid)
                .eq(BarrageBan::getType,type)
                .eq(BarrageBan::getUserId,userId);
        barrageBanMapper.delete(lambdaQueryWrapper);

        return ResponseResult.okResult("取消禁言成功");
    }

    @Override
    public ResponseResult getUserLiveBan(Integer sid, Integer type, Integer userId) {
        LambdaQueryWrapper<BarrageBan> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BarrageBan::getSid,sid)
                .eq(BarrageBan::getType,type)
                .eq(BarrageBan::getUserId,userId);
        BarrageBan barrageBan = barrageBanMapper.selectOne(lambdaQueryWrapper);
        if (!Objects.isNull(barrageBan)){
            return ResponseResult.okResult(1);
        }
        else {
            return ResponseResult.okResult(0);
        }
    }

    @Override
    public ResponseResult getAllLiveBan() {
        List<BarrageBan> barrageBans = barrageBanMapper.selectList(null);
        List<User> userList = (List<User>) userClient.getAllUser().getData();
        List<Agenda> agendaList = (List<Agenda>) agendaService.getAllAgendasBasic().getData();
        List<Activity> activityList = (List<Activity>) activityService.getAllActivityBasic().getData();

        //转为map对象，<userId,userName>
        Map<Integer,String> userMap = new HashMap<>();
        Map<Integer,String> agendaMap = new HashMap<>();
        Map<Integer,String> activityMap = new HashMap<>();
        for (User user : userList) {
            userMap.put(user.getPkId(),user.getUsername());
        }
        for (Agenda agenda : agendaList) {
            agendaMap.put(agenda.getPkId(),agenda.getAgendaName());
        }
        for (Activity activity : activityList) {
            activityMap.put(activity.getPkId(),activity.getActivityName());
        }
        List<BarrageBanDTO> barrageBanDTOList = new ArrayList<>();
        List<BarrageBanDTO> collect = barrageBans.stream().map(item -> {
            BarrageBanDTO barrageBanDTO = new BarrageBanDTO();
            BeanUtils.copyProperties(barrageBans, barrageBanDTO);
            barrageBanDTO.setUserName(userMap.get(item.getPkId()));//设置username

            //标识为议程
            if (item.getType() == 1) {
                barrageBanDTO.setName(agendaMap.get(barrageBanDTO.getSid()));
            } else {
                barrageBanDTO.setName(activityMap.get(barrageBanDTO.getSid()));
            }
            return barrageBanDTO;
        }).collect(Collectors.toList());

        return ResponseResult.okResult(collect);
    }
}
